<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>

<script>
  var lis = document.querySelectorAll('li')
  for (let i = 0; i < lis.length; i++) {
    lis[i].onclick = function () {
      console.log(i)
    }
  }
</script>